<?php
/**
 * Bangding 插件
 * @author 后盾向军 <houdunwangxj@gmail.com>
 */

class AdminController extends AddonController {

    public function index() {
    	$bangding = M('bangding')->order('time desc')->select();
    	$this->assign('data',$bangding);
        $this->display();
    }

     public function styleList()
    {
        $style = array();
        $dirs = Dir::tree('Template');
        foreach ($dirs as $tpl) {
            $xml = $tpl['path'] . '/config.xml';
            if (!is_file($xml)){
                continue;
            }
            if (!$config = Xml::toArray(file_get_contents($xml))){
                continue;
            }
            $tpl['name'] = isset($config['name']) ? $config['name'][0] : ''; //模型名
            $tpl['author'] = isset($config['author']) ? $config['author'][0] : ''; //作者
            $tpl['image'] = isset($config['image']) ? __ROOT__.'/Template/'.$tpl['filename'].'/'.$config['image'][0] : __CONTROLLER_TPL__ . '/img/preview.jpg'; //预览图
            $tpl['email'] = isset($config['email']) ? $config['email'][0] : ''; //邮箱
            $tpl['current'] = C("WEB_STYLE") == $tpl['filename'] ? 1 : 0; //正在使用的模板
            $style[] = $tpl;
        }
     
        return $style;
    }

    public function add(){
    	if(IS_POST) {
    		$data = $_POST;
    		$data['time']= time();
    		$info = M('bangding')->add($data);
    		if($info){
    			go(U('index',array('g'=>'Addon'),''));
    		}else{
    			$this->error('绑定出错');
    		}
    	}else{

	    	$style = $this->styleList();
	    	$this->assign('style',$style);
            
	    	$this->display();
    	}
    	
    }

    /**
     * 编辑域名绑定
     * @return [type] [description]
     */
    public function edit()
    {   
        if(IS_POST)
        {
            $data = $_POST;
            $edit = M('bangding')->data($data)->where(array('id'=>Q('id')))->save();

            if($edit)
            {
                go(U('index',array('g'=>'Addon'),''));
            }else{
                $this->error('保存失败');
            }
        }else{
            $style = $this->styleList();
            $this->assign('style',$style);
            $data = M('bangding')->find(Q('id'));
            $this->assign('data',$data);
            $this->display(); 
        }
        
    }

    public function del() {
    	$id = Q('id');
    	$where = array('id'=>array('in',$id));
    	$info = M('bangding')->where($where)->del();
    	if($info) {
    		go(U('index',array('g'=>'Addon'),''));
    	}
    }

     public function sou() {
        $sou = Q('sou_ip');
        $where['domain'] = array('like',"%$sou%");

        $data = M('bangding')->where($where)->select();
        if(!$data) {
            $this->ajax("");
        }
            $html="";
        foreach ($data as $key => $d) {
            $html.="<tr>";
            if($d['status']==1){
                $status = "√";
            }else{
                $status = "x";
            }
            $html.='<td><input type="checkbox" name="id[]" value="'.$d['id'].'"/></td>
            <td>'.$d['id'].'</td>
            <td>'.$d['domain'].'</td>
            <td>'.$d['webtitle'].'</td>
            <td>'.$d['webaddress'].'</td>
            <td>'.$d['nexturl'].'</td>
            <td>'.$d['wapurl'].'</td>
            <td>'.$status.'</td>
            <td>'.date('Y-m-d H:i:s',$d['time']).'</td>
            <td><a href="'.U('del',array('g' => 'Addons','id'=>$d['id']),'').'">删除</a></td>';
            $html.="</tr>";
        }
        
        $this->ajax($html);
    }

}